package com.aduerr.v1shouyin.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.CheckBillEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.print.PrinterWrapper;
import com.aduerr.v1shouyin.unioncard.BaseUnionActivity;
import com.aduerr.v1shouyin.util.DayWeekMoonTImeUtil;
import com.aduerr.v1shouyin.util.DoubleClickUtils;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.orhanobut.hawk.Hawk;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2020/7/13 9:51
 * @change
 * @chang time
 * @class describe:对账优化迭代后，交易汇总使用此页面
 */

public class CheckBillActivity extends BaseUnionActivity {
    @BindView(R.id.tv_check_bill_reality_value)
    TextView mTvRealityValue;//实收金额
    @BindView(R.id.tv_check_bill_reality_count)
    TextView mTvRealityCount;//实收笔数
    @BindView(R.id.tv_paid_value)
    TextView mTvPaidValue;//订单金额
    @BindView(R.id.tv_paid_count)
    TextView mTvPaidCount;//订单笔数
    @BindView(R.id.tv_preferential_value)
    TextView mTvPreferentialValue;//优惠金额
    @BindView(R.id.tv_preferential_count)
    TextView mTvPreferentialCount;//优惠笔数
    @BindView(R.id.tv_refund_value)
    TextView mTvRefundValue;//退款金额
    @BindView(R.id.tv_refund_count)
    TextView mTvRefundCount;//退款笔数
    //扩大顶部数据点击区域
    @BindView(R.id.ll_Paid)
    LinearLayout mLlPaid;
    @BindView(R.id.ll_preferential)
    LinearLayout mLlPreferential;
    @BindView(R.id.ll_refund)
    LinearLayout mLlRefund;
    //三种选择框
    @BindView(R.id.tv_choose_shop)
    TextView mTvChooseShop;
    @BindView(R.id.iv_choose_shop)
    ImageView mIvChooseShop;
    @BindView(R.id.tv_choose_cashier)
    TextView mTvChooseCashier;
    @BindView(R.id.iv_choose_cashier)
    ImageView mIvChooseCashier;
    @BindView(R.id.tv_choose_time)
    TextView mTvChooseTime;
    @BindView(R.id.iv_choose_time)
    ImageView mIvChooseTime;
    //四个切换
    @BindView(R.id.tv_switch_one)
    TextView mTvSwitchOne;
    @BindView(R.id.tv_switch_two)
    TextView mTvSwitchTwo;
    @BindView(R.id.tv_switch_three)
    TextView mTvSwitchThree;
    @BindView(R.id.tv_switch_four)
    TextView mTvSwitchFour;
    //四个下划线
    @BindView(R.id.iv_indicate_one)
    ImageView mIvIndicateOne;
    @BindView(R.id.iv_indicate_two)
    ImageView mIvIndicateTwo;
    @BindView(R.id.iv_indicate_three)
    ImageView mIvIndicateThree;
    @BindView(R.id.iv_indicate_four)
    ImageView mIvIndicateFour;
    @BindView(R.id.pie_chart)
    PieChart mPieChart;
    //六个横条
    @BindView(R.id.ll_part_one)
    LinearLayout mLlPartOne;
    @BindView(R.id.ll_part_one_right)
    LinearLayout mLlPartOneRight;

    @BindView(R.id.tv_part_one_left)
    TextView mTvPartOneLeft;
    @BindView(R.id.tv_part_one_mid)
    TextView mTvPartOneMid;
    @BindView(R.id.tv_part_one_right)
    TextView mTvPartOneRight;

    @BindView(R.id.ll_part_two)
    LinearLayout mLlPartTwo;
    @BindView(R.id.ll_part_two_right)
    LinearLayout mLlPartTwoRight;

    @BindView(R.id.tv_part_two_left)
    TextView mTvPartTwoLeft;
    @BindView(R.id.tv_part_two_mid)
    TextView mTvPartTwoMid;
    @BindView(R.id.tv_part_two_right)
    TextView mTvPartTwoRight;

    @BindView(R.id.ll_part_three)
    LinearLayout mLlPartThree;
    @BindView(R.id.ll_part_three_right)
    LinearLayout mLlPartThreeRight;

    @BindView(R.id.tv_part_three_left)
    TextView mTvPartThreeLeft;
    @BindView(R.id.tv_part_three_mid)
    TextView mTvPartThreeMid;
    @BindView(R.id.tv_part_three_right)
    TextView mTvPartThreeRight;


    @BindView(R.id.ll_part_four)
    LinearLayout mLlPartFour;
    @BindView(R.id.ll_part_four_right)
    LinearLayout mLlPartFourRight;

    @BindView(R.id.tv_part_four_left)
    TextView mTvPartFourLeft;
    @BindView(R.id.tv_part_four_mid)
    TextView mTvPartFourMid;
    @BindView(R.id.tv_part_four_right)
    TextView mTvPartFourRight;

    @BindView(R.id.ll_part_five)
    LinearLayout mLlPartFive;
    @BindView(R.id.ll_part_five_right)
    LinearLayout mLlPartFiveRight;

    @BindView(R.id.tv_part_five_left)
    TextView mTvPartFiveLeft;
    @BindView(R.id.tv_part_five_mid)
    TextView mTvPartFiveMid;
    @BindView(R.id.tv_part_five_right)
    TextView mTvPartFiveRight;

    @BindView(R.id.ll_part_six)
    LinearLayout mLlPartSix;
    @BindView(R.id.ll_part_six_right)
    LinearLayout mLlPartSixRight;

    @BindView(R.id.tv_part_six_left)
    TextView mTvPartSixLeft;
    @BindView(R.id.tv_part_six_mid)
    TextView mTvPartSixMid;
    @BindView(R.id.tv_part_six_right)
    TextView mTvPartSixRight;

    @BindView(R.id.ll_part_seven)
    LinearLayout mLlPartSeven;
    @BindView(R.id.ll_part_seven_right)
    LinearLayout mLlPartSevenRight;

    @BindView(R.id.tv_part_seven_left)
    TextView mTvPartSevenLeft;
    @BindView(R.id.tv_part_seven_mid)
    TextView mTvPartSevenMid;
    @BindView(R.id.tv_part_seven_right)
    TextView mTvPartSevenRight;
    
    //打印
    @BindView(R.id.tv_print_bill)
    TextView mTvPrintBill;

    @BindView(R.id.ll_choose_shop)
    LinearLayout mLlChooseShop;
    @BindView(R.id.ll_choose_cashier)
    LinearLayout mLlChooseCashier;



    Unbinder unbinder;
    Context context;
    private String begin_time = null;
    private String end_time = null;
    private String shop_id;//商店ID
    private String shop_name = "全部门店";//商店名称
    private String timeName = "今天";//时间名称
    private String seller_name = "全部收银员";//收银员名称
    private String seller_id;//收银员id
    private String userLevel;//角色权限

    private HashMap<String, String> mAddParams = new HashMap<>();


    private String[] arrayColors = {"#3AA0FF", "#37CBCB", "#F2637B", "#FA8537",
            "#FAD337", "#975FE4", "#DFE0E2","#354052"};


    private List<PieEntry> values = new ArrayList<>();//饼图百分比
    private List<Integer> colors = new ArrayList<>();//饼图颜色值

    CheckBillEntity checkBillEntity;

    DecimalFormat df = new DecimalFormat("0.00");

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_bill;
    }


    public void initBase() {
        context = this;
        ButterKnife.bind(this);
        initPicChart();
        initData();
    }

    /**
     * 初始化饼图
     */
    public void initPicChart() {
        PieDataSet dataSet = new PieDataSet(values, "Label");
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        mPieChart.setData(pieData);
        mPieChart.invalidate();
        mPieChart.setTransparentCircleRadius(0f);
        mPieChart.setHoleRadius(88f);
        pieData.setDrawValues(false);
        mPieChart.setDrawSliceText(false);
        Legend legend = mPieChart.getLegend();
        mPieChart.getDescription().setText("");
        legend.setEnabled(false);

    }


    /**
     * 初始化数据
     */
    public void initData() {
        userLevel = Hawk.get("level");
        switch(userLevel) {
            case "客服"://客服和收银员类似
            case "收银员"://收银员
                shop_id = String.valueOf(Hawk.get("shopid", 0));
                shop_name = Hawk.get("shopname", "全部店铺");
                seller_id = String.valueOf(Hawk.get("siteuserid", 0));
                seller_name = Hawk.get("sellername", "全部收银员");
                mIvChooseShop.setVisibility(View.INVISIBLE);
                mIvChooseCashier.setVisibility(View.INVISIBLE);
                mTvChooseShop.setText(shop_name);
                mTvChooseCashier.setText(seller_name);
                mLlChooseCashier.setClickable(false);
                mLlChooseShop.setClickable(false);
                break;
            case "组长"://组长和店长类似
            case "店长"://店长
                shop_id = String.valueOf(Hawk.get("shopid", 0));
                shop_name = Hawk.get("shopname", "全部店铺");
                seller_id = "0";
                mIvChooseShop.setVisibility(View.INVISIBLE);
                mTvChooseShop.setText(shop_name);
                mTvChooseCashier.setText(seller_name);
                mLlChooseCashier.setClickable(true);
                mLlChooseShop.setClickable(false);
                break;
            case "财务专员"://财务专员
                shop_id = "0";
                seller_id = "0";
                mTvChooseShop.setText(shop_name);
                mTvChooseCashier.setText(seller_name);
                mLlChooseCashier.setClickable(true);
                mLlChooseShop.setClickable(true);
                break;

        }


        mTvChooseTime.setText(timeName);
        begin_time = DayWeekMoonTImeUtil.getTodayBeginTime();
        end_time = DayWeekMoonTImeUtil.getTodayEndTime();
        mAddParams.put("StartTime", begin_time);
        mAddParams.put("EndTime", end_time);
        mAddParams.put("ShopID", shop_id);
        mAddParams.put("SiteUserID", seller_id);
        getData(mAddParams);

    }


    @OnClick({R.id.img_back,R.id.ll_Paid, R.id.ll_preferential, R.id.ll_refund, R.id.ll_choose_shop,
            R.id.ll_choose_cashier, R.id.ll_choose_time, R.id.tv_switch_one,
            R.id.tv_switch_two, R.id.tv_switch_three, R.id.tv_switch_four,
            R.id.ll_part_one_right, R.id.ll_part_two_right, R.id.ll_part_three_right,
            R.id.ll_part_four_right, R.id.ll_part_five_right, R.id.ll_part_six_right,
            R.id.tv_print_bill})
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_choose_shop:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.ll_choose_shop)) {

                    Intent shop_intent = new Intent(this, HomeBillSelectActivity.class);
                    shop_intent.putExtra("select_type", 0);

                    startActivityForResult(shop_intent, 66);
                }

                break;

            case R.id.ll_choose_cashier:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.ll_choose_cashier)) {

                    Intent shop_intent = new Intent(this, HomeBillSelectActivity.class);
                    shop_intent.putExtra("select_type", 1);

                    startActivityForResult(shop_intent, 67);
                }

                break;
            case R.id.ll_choose_time:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.ll_choose_time)) {
                    Intent intent = new Intent(this, HomeBillTimeSelectActivity.class);
                    startActivityForResult(intent, 68);
                }

                break;

            case R.id.tv_switch_one:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.tv_switch_one)) {
                    switchIndicate(R.id.tv_switch_one);
                    setDetailBill(0, checkBillEntity);

                }

                break;

            case R.id.tv_switch_two:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.tv_switch_two)) {
                    switchIndicate(R.id.tv_switch_two);
                    setDetailBill(1, checkBillEntity);
                }

                break;

            case R.id.tv_switch_three:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.tv_switch_three)) {
                    switchIndicate(R.id.tv_switch_three);
                    setDetailBill(2, checkBillEntity);
                }

                break;

            case R.id.tv_switch_four:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.tv_switch_four)) {
                    switchIndicate(R.id.tv_switch_four);
                    setDetailBill(3, checkBillEntity);
                }

                break;

            case R.id.tv_print_bill:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.tv_print_bill)) {
                    printText();
                }
                break;
        }

    }

    public void switchIndicate(int tvId) {
        mTvSwitchOne.setTextAppearance(this, tvId == R.id.tv_switch_one ?
                R.style.text_switch_one : R.style.text_switch_two);
        mTvSwitchTwo.setTextAppearance(this, tvId == R.id.tv_switch_two ?
                R.style.text_switch_one : R.style.text_switch_two);
        mTvSwitchThree.setTextAppearance(this, tvId == R.id.tv_switch_three ?
                R.style.text_switch_one : R.style.text_switch_two);
        mTvSwitchFour.setTextAppearance(this, tvId == R.id.tv_switch_four ?
                R.style.text_switch_one : R.style.text_switch_two);

        mIvIndicateOne.setVisibility(tvId == R.id.tv_switch_one ? View.VISIBLE :
                View.INVISIBLE);
        mIvIndicateTwo.setVisibility(tvId == R.id.tv_switch_two ? View.VISIBLE :
                View.INVISIBLE);
        mIvIndicateThree.setVisibility(tvId == R.id.tv_switch_three ? View.VISIBLE :
                View.INVISIBLE);
        mIvIndicateFour.setVisibility(tvId == R.id.tv_switch_four ? View.VISIBLE :
                View.INVISIBLE);


    }


    /**
     * 获取本页面全部数据
     */
    private void getData(HashMap<String, String> addParams) {
        APIRetrofit.getAPIService().getOrderReport(RXRequest.getXingPosParams(addParams,
                context))
                .subscribeOn(Schedulers.io())//开启一个io线程
                .observeOn(AndroidSchedulers.mainThread())//回到主线程
                .subscribe(new Observer<CheckBillEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CheckBillEntity entity) {
                        if(entity == null || entity.getData() == null) {
                            return;
                        }
                        checkBillEntity = entity;
                        mTvRealityValue.setText(df.format(checkBillEntity.getData
                                ().getPayMoney()));//实收金额
                        mTvRealityCount.setText("实收笔数:" + String.valueOf(checkBillEntity.getData
                                ().getPayCount()));//实收笔数
                        mTvPaidValue.setText(df.format(checkBillEntity.getData()
                                .getOrderMoney()));//订单金额
                        mTvPaidCount.setText("笔数:" + String.valueOf(checkBillEntity.getData()
                                .getPayCount()));//订单笔数
                        mTvPreferentialValue.setText(df.format(checkBillEntity
                                .getData().getDiscountMoney()));//优惠金额
                        mTvPreferentialCount.setText("笔数:" + String.valueOf(checkBillEntity
                                .getData().getDiscountCount()));//优惠笔数
                        mTvRefundValue.setText(df.format(checkBillEntity.getData()
                                .getRefundMoney()));//退款金额
                        mTvRefundCount.setText("笔数:" + String.valueOf(checkBillEntity.getData()
                                .getRefundCount()));//退款笔数

                        setDetailBill(0, checkBillEntity);
                        switchIndicate(R.id.tv_switch_one);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("CheckBillActivity", e.getMessage() + "");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }



    /**
     * 给明细条目赋值，隐藏不需要的条目
     */
    public void setDetailBill(int switchType, CheckBillEntity checkBillEntity) {
        values.clear();
        colors.clear();
        switch(switchType) {
            case 0://第1切换页
                mLlPartOne.setVisibility(View.VISIBLE);
                mLlPartTwo.setVisibility(View.VISIBLE);
                mLlPartThree.setVisibility(View.VISIBLE);
                mLlPartFour.setVisibility(View.VISIBLE);
                mLlPartFive.setVisibility(View.VISIBLE);
                mLlPartSix.setVisibility(View.VISIBLE);
                mLlPartSeven.setVisibility(View.VISIBLE);

                mLlPartOneRight.setClickable(true);
                mLlPartTwoRight.setClickable(true);
                mLlPartThreeRight.setClickable(true);
                mLlPartFourRight.setClickable(true);
                mLlPartFiveRight.setClickable(true);
                mLlPartSixRight.setClickable(true);
                mLlPartSevenRight.setClickable(true);
                
                mTvPartOneLeft.setText("支付宝(" + checkBillEntity.getData().getAlipayPayCount() + ")笔");
                mTvPartOneMid.setText(checkBillEntity.getData().getAlipayPercent() + "%");
                mTvPartOneRight.setText("￥" + df.format(checkBillEntity.getData().getAlipayPayMoney()));

                mTvPartTwoLeft.setText("微信(" + checkBillEntity.getData().getWeiXinPayCount() + ")笔");
                mTvPartTwoMid.setText(checkBillEntity.getData().getWeiXinPercent() + "%");
                mTvPartTwoRight.setText("￥" + df.format(checkBillEntity.getData().getWeiXinPayMoney()));

                mTvPartThreeLeft.setText("云闪付(" + checkBillEntity.getData().getYsfPayCount() + ")笔");
                mTvPartThreeMid.setText(checkBillEntity.getData().getYsfPercent() + "%");
                mTvPartThreeRight.setText("￥" + df.format(checkBillEntity.getData().getYsfPayMoney()));

                mTvPartFourLeft.setText("银联(" + checkBillEntity.getData().getBankPayCount() + ")笔");
                mTvPartFourMid.setText(checkBillEntity.getData().getBankPercent() + "%");
                mTvPartFourRight.setText("￥" + df.format(checkBillEntity.getData().getBankPayMoney()));

                mTvPartFiveLeft.setText("数币(" + checkBillEntity.getData().getDcPayCount() + ")笔");
                mTvPartFiveMid.setText(checkBillEntity.getData().getDcPercent() + "%");
                mTvPartFiveRight.setText("￥" + df.format(checkBillEntity.getData().getDcPayMoney()));

                mTvPartSixLeft.setText("现金(" + checkBillEntity.getData().getCashPayCount() + ")笔");
                mTvPartSixRight.setText(checkBillEntity.getData().getCashPercent() + "%");
                mTvPartFiveRight.setText("￥" + df.format(checkBillEntity.getData().getCashPayMoney()));

                mTvPartSevenLeft.setText("其它(" + checkBillEntity.getData().getOtherPayCount() + ")笔");
                mTvPartSevenMid.setText(checkBillEntity.getData().getOtherPercent() + "%");
                mTvPartSevenRight.setText("￥" + df.format(checkBillEntity.getData().getOtherPayMoney()));


            
                mPieChart.setCenterText(getText("实收金额\n" + df.format(checkBillEntity.getData().getPayMoney())));
                values.add(new PieEntry(checkBillEntity.getData().getAlipayPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getWeiXinPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getYsfPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getBankPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getCashPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getOtherPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getDcPercent(), ""));


                break;

            case 1://第2切换页
                mLlPartOne.setVisibility(View.VISIBLE);
                mLlPartTwo.setVisibility(View.VISIBLE);
                mLlPartThree.setVisibility(View.VISIBLE);
                mLlPartFour.setVisibility(View.INVISIBLE);
                mLlPartFive.setVisibility(View.INVISIBLE);
                mLlPartSix.setVisibility(View.INVISIBLE);
                mLlPartSeven.setVisibility(View.INVISIBLE);

                mLlPartOneRight.setClickable(false);
                mLlPartTwoRight.setClickable(false);
                mLlPartThreeRight.setClickable(false);
                mLlPartFourRight.setClickable(false);
                mLlPartFiveRight.setClickable(false);
                mLlPartSixRight.setClickable(false);
                mLlPartSevenRight.setClickable(false);

         

                mTvPartOneLeft.setText("营销红包(" + checkBillEntity.getData().getHongBaoCount() + ")笔");
                mTvPartOneMid.setText(checkBillEntity.getData().getHongBaoPercent() + "%");
                mTvPartOneRight.setText("￥" + df.format(checkBillEntity.getData().getHongBaoMoney()));

                mTvPartTwoLeft.setText("商家折扣(" + checkBillEntity.getData().getLevelDiscountCount() + ")笔");
                mTvPartTwoMid.setText(checkBillEntity.getData().getLevelDiscountPercent() + "%");
                mTvPartTwoRight.setText("￥" + df.format(checkBillEntity.getData().getLevelDiscountMoney()));

                mTvPartThreeLeft.setText("核销金额(" + checkBillEntity.getData().getCouponCount() + ")笔");
                mTvPartThreeMid.setText(checkBillEntity.getData().getCouponPercent() +
                        "%");
                mTvPartThreeRight.setText("￥" + df.format(checkBillEntity.getData().getCouponMoney()));

                values.add(new PieEntry(checkBillEntity.getData().getHongBaoPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getLevelDiscountPercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getCouponPercent(), ""));

                mPieChart.setCenterText(getText("优惠明细\n" + df.format(checkBillEntity.getData().getDiscountMoney())));
                break;

            case 2://第3切换页
                mLlPartOne.setVisibility(View.VISIBLE);
                mLlPartTwo.setVisibility(View.VISIBLE);
                mLlPartThree.setVisibility(View.INVISIBLE);
                mLlPartFour.setVisibility(View.INVISIBLE);
                mLlPartFive.setVisibility(View.INVISIBLE);
                mLlPartSix.setVisibility(View.INVISIBLE);
                mLlPartSeven.setVisibility(View.INVISIBLE);

                mLlPartOneRight.setClickable(false);
                mLlPartTwoRight.setClickable(false);
                mLlPartThreeRight.setClickable(false);
                mLlPartFourRight.setClickable(false);
                mLlPartFiveRight.setClickable(false);
                mLlPartSevenRight.setClickable(false);

    

                mTvPartOneLeft.setText("扫码退款(" + checkBillEntity.getData().getScanRefundCount() + ")笔");
                mTvPartOneMid.setText(checkBillEntity.getData().getScanRefundPercent() +
                        "%");
                mTvPartOneRight.setText("￥" + df.format(checkBillEntity.getData().getScanRefundMoney()));

                mTvPartTwoLeft.setText("银联退款(" + checkBillEntity.getData().getBankRefundCount() + ")笔");
                mTvPartTwoMid.setText(checkBillEntity.getData().getBankRefundPercent() +
                        "%");
                mTvPartTwoRight.setText("￥" + df.format(checkBillEntity.getData().getBankRefundMoney()));

                values.add(new PieEntry(checkBillEntity.getData().getScanRefundPercent(),
                        ""));
                values.add(new PieEntry(checkBillEntity.getData().getBankRefundPercent(),
                        ""));
                mPieChart.setCenterText(getText("退款明细\n" + df.format(checkBillEntity.getData().getRefundMoney())));
                break;

            case 3://第4切换页
                mLlPartOne.setVisibility(View.VISIBLE);
                mLlPartTwo.setVisibility(View.VISIBLE);
                mLlPartThree.setVisibility(View.VISIBLE);
                mLlPartFour.setVisibility(View.VISIBLE);
                mLlPartFive.setVisibility(View.VISIBLE);
                mLlPartSix.setVisibility(View.INVISIBLE);
                mLlPartSeven.setVisibility(View.INVISIBLE);

                mLlPartOneRight.setClickable(false);
                mLlPartTwoRight.setClickable(false);
                mLlPartThreeRight.setClickable(false);
                mLlPartFourRight.setClickable(false);
                mLlPartFiveRight.setClickable(false);
                mLlPartSevenRight.setClickable(false);

                

                mTvPartOneLeft.setText("会员卡充值(" + checkBillEntity.getData().getMemberRechargeCount() + ")笔");
                mTvPartOneMid.setText(checkBillEntity.getData().getMemberRechargePercent() + "%");
                mTvPartOneRight.setText("￥" + df.format(checkBillEntity.getData().getMemberRechargeMoney()));

                mTvPartTwoLeft.setText("会员卡消费(" + checkBillEntity.getData().getMemberConsumeCount() + ")笔");
                mTvPartTwoMid.setText(checkBillEntity.getData().getMemberConsumePercent() + "%");
                mTvPartTwoRight.setText("￥" + df.format(checkBillEntity.getData().getMemberConsumeMoney()));

                mTvPartThreeLeft.setText("银联预授权(" + checkBillEntity.getData().getBankDepositCount() + ")笔");
                mTvPartThreeMid.setText(checkBillEntity.getData().getBankDepositPercent() + "%");
                mTvPartThreeRight.setText("￥" + df.format(checkBillEntity.getData().getBankDepositMoney()));

                mTvPartFourLeft.setText("扫码预授权(" + checkBillEntity.getData().getScanDepositCount() + ")笔");
                mTvPartFourMid.setText(checkBillEntity.getData().getScanDepositPercent() + "%");
                mTvPartFourRight.setText("￥" + df.format(checkBillEntity.getData().getScanDepositMoney()));

                mTvPartFiveLeft.setText("预估手续费");
                mTvPartFiveMid.setText(checkBillEntity.getData().getSellerFeePercent() +
                        "%");
                mTvPartFiveRight.setText("￥" + df.format(checkBillEntity.getData().getSellerFee()));

                values.add(new PieEntry(checkBillEntity.getData().getMemberRechargePercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getMemberConsumePercent(), ""));
                values.add(new PieEntry(checkBillEntity.getData().getBankDepositPercent()
                        , ""));
                values.add(new PieEntry(checkBillEntity.getData().getScanDepositPercent(),
                        ""));
                values.add(new PieEntry(checkBillEntity.getData().getSellerFeePercent(),
                        ""));
                double totalOther =
                        checkBillEntity.getData().getMemberRechargeMoney() + checkBillEntity.getData().getMemberConsumeMoney() + checkBillEntity.getData().getBankDepositMoney() + checkBillEntity.getData().getScanDepositMoney() + checkBillEntity.getData().getSellerFee();
                mPieChart.setCenterText(getText("其他金额\n" +df.format(totalOther)));


                break;


        }
        float totalValues = 0;
        for(int i = 0; i < values.size(); i++) {
            totalValues = totalValues + values.get(i).getValue();
            colors.add(Color.parseColor(arrayColors[i]));
            Log.d("co112", arrayColors[i]);
        }
        if(totalValues == 0) {
            values.clear();
            colors.clear();
            values.add(new PieEntry(100f, "暂无数据"));
            colors.add(Color.parseColor(arrayColors[6]));
        }

        mPieChart.notifyDataSetChanged();
        mPieChart.invalidate();
    }



    /**
     * 饼图中间的字要使用富文本改变样式达到UI效果
     * @param money
     * @return
     */
    public SpannableString getText(String money) {
        String text = money;
        SpannableString builder = new SpannableString(text);
        builder.setSpan(new ForegroundColorSpan(Color.parseColor("#A7AFC5")), 0, 4,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(new RelativeSizeSpan(1.5f), 0, 4,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        builder.setSpan(new ForegroundColorSpan(Color.parseColor("#364571")), 5,
                text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        //设置文字大小自动变化。默认用户最大显示值为1000万+"实收金额"=共15位，每少一位，相对大小增加0.1
        builder.setSpan(new RelativeSizeSpan((15-text.length())*0.2f+2f), 5, text.length(),
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);//
        builder.setSpan(new StyleSpan(Typeface.BOLD), 5, text.length() ,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return builder;

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //从选择器的界面带来数据  在onresume();中进行请求网路.
        switch(requestCode) {
            //门店
            case 66:
                if(data != null) {
                    shop_id = data.getStringExtra("shop_id");
                    shop_name = data.getStringExtra("shop_name");
                    mAddParams.put("ShopID", shop_id);
                    mTvChooseShop.setText(shop_name);
                    //每次选择完门店后需要重置   收银员
                    mTvChooseCashier.setText("全部收银员");
                    seller_id = "0";
                    mAddParams.put("SiteUserID", "0");
                    mAddParams.put("StartTime", begin_time);
                    mAddParams.put("EndTime", end_time);
                    getData(mAddParams);

                }
                break;
            //收银员
            case 67:
                if(data != null) {
                    seller_id = data.getStringExtra("seller_id");
                    seller_name = data.getStringExtra("seller_name");
                    mAddParams.put("SiteUserID", seller_id);
                    mAddParams.put("ShopID", shop_id);
                    mTvChooseCashier.setText(seller_name);
                    mAddParams.put("StartTime", begin_time);
                    mAddParams.put("EndTime", end_time);
                    getData(mAddParams);
                }
                break;
            //时间
            case 68:
                if(data != null) {
                    timeName = data.getStringExtra("time_id");
                    begin_time = data.getStringExtra("begin_time");
                    end_time = data.getStringExtra("end_time");
                    mAddParams.put("StartTime", begin_time);
                    mAddParams.put("EndTime", end_time);
                    mAddParams.put("SiteUserID", seller_id);
                    mAddParams.put("ShopID", shop_id);
                    //将时间存起来
                    mTvChooseTime.setText(timeName);
                    getData(mAddParams);
                }
                break;
        }
    }


    public void printText() {

        SimpleDateFormat  sdFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String currTime = sdFormatter.format(curDate);
        HashMap<String, String> printMap = new HashMap<>();
        printMap.put("headText", "对账");
        printMap.put("BeginTime", begin_time);
        printMap.put("EndTime", end_time);
        printMap.put("BusinessInfo", Hawk.get("company", "未查到"));
        printMap.put("Shop", shop_name);
        printMap.put("Seller", seller_name);
        printMap.put("Terminal", "POS");


        printMap.put("OrderCount", checkBillEntity.getData().getPayCount() + "");
        printMap.put("OrderMoney", df.format(checkBillEntity.getData().getOrderMoney()));
        printMap.put("DiscountCount", checkBillEntity.getData().getDiscountCount() + "");
        printMap.put("DiscountMoney", df.format(checkBillEntity.getData().getDiscountMoney()));

        printMap.put("HongBaoMoney", df.format(checkBillEntity.getData().getHongBaoMoney()));
        printMap.put("HongBaoCount", checkBillEntity.getData().getHongBaoCount() + "");
        printMap.put("LevelDiscountMoney",
                df.format(checkBillEntity.getData().getLevelDiscountMoney()));
        printMap.put("LevelDiscountCount",
                checkBillEntity.getData().getLevelDiscountCount() + "");
        printMap.put("CouponMoney",df.format(checkBillEntity.getData().getCouponMoney()));
        printMap.put("CouponCount", checkBillEntity.getData().getCouponCount() + "");

        printMap.put("RefundCount", checkBillEntity.getData().getRefundCount() + "");
        printMap.put("RefundMoney", df.format(checkBillEntity.getData().getRefundMoney()));
        printMap.put("ScanRefundCount",
                checkBillEntity.getData().getScanRefundCount() + "");
        printMap.put("ScanRefundMoney",
                df.format(checkBillEntity.getData().getScanRefundMoney()));
        printMap.put("BankRefundCount",
                checkBillEntity.getData().getBankRefundCount() + "");
        printMap.put("BankRefundMoney",
                df.format(checkBillEntity.getData().getBankRefundMoney()));

        printMap.put("PayCount",
                checkBillEntity.getData().getPayCount() + "");
        printMap.put("PayMoney",
                df.format(checkBillEntity.getData().getPayMoney()));
        printMap.put("AlipayPayCount",
                checkBillEntity.getData().getAlipayPayCount() + "");
        printMap.put("AlipayPayMoney",
                df.format(checkBillEntity.getData().getAlipayPayMoney()));
        printMap.put("WeiXinPayCount",
                checkBillEntity.getData().getWeiXinPayCount() + "");
        printMap.put("WeiXinPayMoney",
                df.format(checkBillEntity.getData().getWeiXinPayMoney()));
        printMap.put("YsfPayCount", checkBillEntity.getData().getYsfPayCount() + "");
        printMap.put("YsfPayMoney", df.format(checkBillEntity.getData().getYsfPayMoney()));
        printMap.put("BankPayCount",
                checkBillEntity.getData().getBankPayCount() + "");
        printMap.put("BankPayMoney",
                df.format(checkBillEntity.getData().getBankPayMoney()));

        printMap.put("DigitalCount", checkBillEntity.getData().getDcPayCount() + "");
        printMap.put("DigitalMoney", df.format(checkBillEntity.getData().getDcPayMoney()));

        printMap.put("CashPayCount", checkBillEntity.getData().getCashPayCount() + "");
        printMap.put("CashPayMoney", df.format(checkBillEntity.getData().getCashPayMoney()));
        printMap.put("OtherPayCount", checkBillEntity.getData().getOtherPayCount() + "");
        printMap.put("OtherPayMoney", df.format(checkBillEntity.getData().getOtherPayMoney()));


        printMap.put("MemberRechargeCount",
                checkBillEntity.getData().getMemberRechargeCount() + "");
        printMap.put("MemberRechargeMoney",
                df.format(checkBillEntity.getData().getMemberRechargeMoney()));
        printMap.put("MemberConsumeCount",
                checkBillEntity.getData().getMemberConsumeCount() + "");
        printMap.put("MemberConsumeMoney",
                df.format(checkBillEntity.getData().getMemberConsumeMoney()));
        printMap.put("BankDepositCount",
                checkBillEntity.getData().getBankDepositCount() + "");
        printMap.put("BankDepositMoney",
                df.format(checkBillEntity.getData().getBankDepositMoney()));
        printMap.put("ScanDepositCount",
                checkBillEntity.getData().getScanDepositCount() + "");
        printMap.put("ScanDepositMoney",
                df.format(checkBillEntity.getData().getScanDepositMoney()));
        printMap.put("SellerFee", checkBillEntity.getData().getSellerFee() + "");


        int printPage = (int) SharedPreferencesUtils.getParam(this, "printpage", 1);
        if(printPage == 0) {
            ToastUtils.showToast(this, "打印张数设置为不打印,请先设置打印张数!!");
            return;
        }

            PrinterWrapper.printCheckBill(printMap,printPage);

    }
    
    

    @Override
    protected void onDestroy() {
        if(unbinder != null) {
            unbinder.unbind();
        }
        super.onDestroy();
    }
}
